package com.scpii.api.common.json.serializer;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

import com.scpii.api.common.base.Response;

public class ResponseSerializer extends JsonSerializer<Response> {

	@Override
	public void serialize(Response value, JsonGenerator jgen,
			SerializerProvider provider) throws IOException,
			JsonProcessingException {
		jgen.writeStartObject();
		jgen.writeNumberField("count", value.getCount());
		jgen.writeNumberField("startIndex", value.getStartIndex());
		jgen.writeNumberField("nextIndex", value.getNextIndex());
		jgen.writeNumberField("status", value.getStatus().getValue());
		jgen.writeStringField("statusDescription", value.getStatusDescription());
		if (!StringUtils.isBlank(value.getTips())) {
			jgen.writeStringField("tips", value.getTips());
		}
		if (value.getResponse() == null) {
			jgen.writeFieldName("response");
			jgen.writeStartObject();
			jgen.writeEndObject();
		} else {
			jgen.writeObjectField("response", value.getResponse());
		}
		jgen.writeEndObject();
	}
}
